package com.yogurt.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.internal.$Gson$Preconditions;
import com.yogurt.edu.entity.subject.OneSubject;
import com.yogurt.edu.entity.subject.TwoSubject;
import com.yogurt.edu.listener.SubjectExcelListener;
import com.yogurt.edu.entity.Subject;
import com.yogurt.edu.entity.excel.SubjectData;
import com.yogurt.edu.mapper.SubjectMapper;
import com.yogurt.edu.service.SubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author yogurt
 * @since 2020-07-09
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    //添加课程分类
    @Override
    public void add(MultipartFile file,SubjectService subjectService) {
        try {
            //获取文件输入流
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //课程分类列表
    @Override
    public List<OneSubject> findAllSubject() {
        //创建最终存放数据的集合
        List<OneSubject> result = new ArrayList<>();

        //1.查询所有的一级分类 parent_id = 0
        QueryWrapper<Subject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        //所有的一级分类
        List<Subject> subjectOne = baseMapper.selectList(wrapperOne);

        //2.查询所有的二级分类 parent_id != 0
        QueryWrapper<Subject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        //所有的二级分类
        List<Subject> subjectsTwo = baseMapper.selectList(wrapperTwo);

        /*返回的为Subject对象,需要封装的为OneSubject对象
            1.将subjectOne所有的一级分类进行遍历，得到每一个Subject对象
            2.将Subject对象中的值取出来，封装到OneSubject对象中 （id，title）
            3.再将OneSubject对象放入最终的集合中 List<OneSubject> result
         */

        //创建OneSubject对象
        OneSubject oneSubject = null;
        for (Subject subject : subjectOne) {
            oneSubject = new OneSubject();

            /* 方法一
            oneSubject.setId(subject.getId()); //设置一级分类的id
            oneSubject.setTitle(subject.getTitle());//设置一级分类的title
            */

            //使用工具类的方式，效果和方法一一样  （方法二）
            BeanUtils.copyProperties(subject,oneSubject);
            //将所有的一级分类放入集合中
            //result.add(oneSubject);

            /*得到所有的二级分类集合
                0.创建一个存放TwoSubject对象的集合,存放所有的TwoSubject对象
                1.循环遍历所有的二级分类,得到每一个subjectTwo对象
                2.创建TwoSubject对象，设置其id和title值
                3.将所有的TwoSubject对象放入TwoSubject对象的集合中
                4.将TwoSubject的集合放入OneSubject对象中
                5.将OneSubject对象放入最终的list集合result中
             */

            //创建存放TwoSubject对象的集合
            List<TwoSubject> twoSubjectList = new ArrayList<>();
            //创建TwoSubject对象
            TwoSubject twoSubject = null;
            for (Subject subjectTwo : subjectsTwo) {
                twoSubject = new TwoSubject();
                /*
                twoSubject.setId(subjectTwo.getId());
                twoSubject.setTitle(subjectTwo.getTitle());
                 */
                //判断一级分类下包含那些二级分类
                if (subjectTwo.getParentId().equals(subject.getId())){
                    BeanUtils.copyProperties(subjectTwo,twoSubject);
                    twoSubjectList.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoSubjectList);
            result.add(oneSubject);
        }
        return result;
    }
}
